package com.easyadmin.pro.modules.sys.service.impl;

import com.easyadmin.pro.modules.sys.entity.UserEntity;
import com.easyadmin.pro.modules.sys.mapper.UserMapper;
import com.easyadmin.pro.modules.sys.mapper.UserRoleMapper;
import com.easyadmin.pro.modules.sys.query.UserInfoQuery;
import com.easyadmin.pro.modules.sys.rest.UserInfoResultVo;
import com.easyadmin.pro.modules.sys.service.IUserService;
import com.easyadmin.pro.modules.sys.vo.UserInfoVo;
import com.easyadmin.pro.modules.sys.vo.UserRoleVo;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author 骑着蚂蚁去上天
 * @since 2024-04-01
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {


    private final UserRoleMapper userRoleMapper;

    @Override
    public UserInfoVo getUserInfo(String userId) {
        // 获取用户信息
        UserInfoVo userInfoVo = this.getOneAsOpt(new QueryWrapper()
                .select(UserEntity::getUserName, UserEntity::getTrueName, UserEntity::getStatus)
                .eq(UserEntity::getId, userId), UserInfoVo.class).get();
        UserRoleVo userRoleVo = userRoleMapper.selectUserRoleInfo(userId);
        userInfoVo.setRole(userRoleVo);
        return userInfoVo;
    }

    @Override
    public List<UserInfoResultVo> queryUserInfo(UserInfoQuery userInfoQuery) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (StringUtils.hasLength(userInfoQuery.getUserName())) {
            queryWrapper.like(UserEntity::getUserName, userInfoQuery.getUserName());
        }

        if (StringUtils.hasLength(userInfoQuery.getTrueName())) {
            queryWrapper.like(UserEntity::getTrueName, userInfoQuery.getTrueName());
        }
        return listAs(queryWrapper, UserInfoResultVo.class);
    }
}
